package com.example.administrator_kunker.mjj_pro;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import com.example.administrator_kunker.mjj_pro.util.AndroidClient;

public class ApplicationLocal extends Application{

    private SharedPreferences sp;
    private AndroidClient aclient ;
    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    private void init() {
        sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        String ip = (sp.contains("ip"))?sp.getString("ip",""):"127.0.0.1";
        String port = (sp.contains("port"))?sp.getString("port",""):"9111";
        aclient = AndroidClient.getInstance(ip,port);
        aclient.connect();
    }

    public void updateConfig(){
        String ip = (sp.contains("ip"))?sp.getString("ip",""):"127.0.0.1";
        String port = (sp.contains("port"))?sp.getString("port",""):"9111";
        aclient.setIp(ip);
        aclient.setPort(port);
    }

    public AndroidClient getAclient() {
        return aclient;
    }

    public void disConnect(){
        aclient.destory();
    }

    public void reConnect(){
        aclient.connect();
    }

    public boolean state(){
        return aclient.state();
    }
}
